package com.ruoyi.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ByteUtils {
    private static final Logger log = LoggerFactory.getLogger(ByteUtils.class);
    public static int loword(int i) {
        return i & 0xFFFF;
    }

    /**
     * 取高位
     *
     * @param i
     * @return
     */
    public static int hiword(int i) {
        return (i & 0xffff0000) >> 16;
    }

    public static byte[] concat(byte[] a, byte[] b) {
        if (a == null) {
            return b;
        } else if (b == null) {
            return a;
        } else {
            byte[] c = new byte[a.length + b.length];
            System.arraycopy(a, 0, c, 0, a.length);
            System.arraycopy(b, 0, c, a.length, b.length);
            return c;
        }
    }

    public void bytesToHexString(byte[] bytes) {
        StringBuilder hexStringBuilder = new StringBuilder();
        for (byte b : bytes) {
            // Convert the byte to an unsigned int and format it as a two-digit hexadecimal
            hexStringBuilder.append(String.format("%02X", b));
        }
        log.info(hexStringBuilder.toString());
    }

    public int byteToPositiveInt(byte b){
        int num = b & 0xFF;
        return num;
    }

}
